<template>
  <el-dialog
    v-model="visible"
    :title="`${paramsProps.title}`"
    :destroy-on-close="true"
    width="580px"
    draggable
  >
    <el-form
      ref="ruleFormRef"
      label-width="140px"
      label-suffix=" :"
      :rules="rules"
      :model="paramsProps.row"
      @submit.enter.prevent="handleSubmit"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="paramsProps.row.title"
          placeholder="请填写标题"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="标题注释" prop="titleUnder">
        <el-input
          v-model="paramsProps.row.titleUnder"
          placeholder="请填写标题注释"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="状态:0草稿,1已保存" prop="status">
        <el-select v-model="paramsProps.row.status" clearable placeholder="请选择状态:0草稿,1已保存">
          <el-option
            v-for="item in optionsStore.getDictOptions('')"
            :key="item.id"
            :label="item.codeName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="简介" prop="context">
        <el-input
          v-model="paramsProps.row.context"
          placeholder="请填写简介"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="主体音乐文件ID" prop="musicFile">
        <el-input
          v-model="paramsProps.row.musicFile"
          placeholder="请填写主体音乐文件ID"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="封面地址" prop="ext1">
        <el-input
          v-model="paramsProps.row.ext1"
          placeholder="请填写封面地址"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false"> 取消</el-button>
      <el-button type="primary" @click="handleSubmit"> 确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { type ElForm, ElMessage } from 'element-plus'
import { useOptionsStore } from '@/stores/modules/options'

defineOptions({
    name: 'DetailModeHeadForm'
})

const optionsStore = useOptionsStore()
const rules = reactive({
})

const visible = ref(false)
const paramsProps = ref<View.DefaultParams>({
  title: '',
  row: {},
  api: undefined,
  getTableList: undefined
})

// 接收父组件传过来的参数
const acceptParams = (params: View.DefaultParams) => {
  paramsProps.value = params
  visible.value = true
}

// 提交数据（新增/编辑）
const ruleFormRef = ref<InstanceType<typeof ElForm>>()
const handleSubmit = () => {
  ruleFormRef.value!.validate(async (valid) => {
    if (!valid) return
    try {
      await paramsProps.value.api!(paramsProps.value.row)
      ElMessage.success({ message: `${paramsProps.value.title}成功！` })
      paramsProps.value.getTableList!()
      visible.value = false
    } catch (error) {
      console.log(error)
    }
  })
}

defineExpose({
  acceptParams
})
</script>

<style scoped lang="scss"></style>